package nus.iss.laps;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import nus.iss.laps.cri.Criteria;
import nus.iss.laps.dao.EmployeeDAO;
import nus.iss.laps.dao.LeavesentitlementDAO;
import nus.iss.laps.dao.UserDAO;
import nus.iss.laps.general.RecordStatus;
import nus.iss.laps.model.Employee;
import nus.iss.laps.model.Leavesentitlement;
import nus.iss.laps.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

@Controller
public class EmployeeController {
	
	@Autowired
	EmployeeDAO employeeDAO;
	
	@Autowired
	UserDAO userDAO;
	
	@Autowired
	LeavesentitlementDAO leavesentitlementDAO;

	@RequestMapping(value = "/employees", method = RequestMethod.GET)
	public String employeeList(Locale locale, Model model) {
		try {
			Criteria l_cri = new Criteria();
			l_cri.setRowIndex(0);
			l_cri.setRowLimit(5);
			
			List<Employee> l_listEmp = employeeDAO.getEmployees(l_cri);
			model.addAttribute("emplist", l_listEmp);
			model.addAttribute("criteria", l_cri);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "list_user";
	}
	
	@RequestMapping(value = "/employees", method = RequestMethod.POST)
	public String rowLimitChange(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setRowIndex(0);
			l_cri.setKeyword(webRequest.getParameter("keyword"));
			l_cri.setRowLimit(Integer.parseInt(webRequest.getParameter("cboRows")));
			
			List<Employee> l_listEmp = employeeDAO.getEmployees(l_cri);
			model.addAttribute("emplist", l_listEmp);
			model.addAttribute("criteria", l_cri);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "list_user";
	}
	
	@RequestMapping(value = "/employees", method = RequestMethod.POST, params = "btnNext")
	public String next(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setKeyword(webRequest.getParameter("keyword"));
			l_cri.setRowLimit(Integer.parseInt(webRequest.getParameter("cboRows")));
			l_cri.setRowIndex(Integer.parseInt(webRequest.getParameter("rowIndex")) + l_cri.getRowLimit());
			List<Employee> l_listEmployee = employeeDAO.getEmployees(l_cri);
			model.addAttribute("criteria", l_cri);
			model.addAttribute("emplist", l_listEmployee);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "list_user";
	}
	
	@RequestMapping(value = "/employees", method = RequestMethod.POST, params = "btnPrev")
	public String previous(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setKeyword(webRequest.getParameter("keyword"));
			l_cri.setRowLimit(Integer.parseInt(webRequest.getParameter("cboRows")));
			l_cri.setRowIndex(Integer.parseInt(webRequest.getParameter("rowIndex")) - l_cri.getRowLimit());
			List<Employee> l_listEmployee = employeeDAO.getEmployees(l_cri);
			model.addAttribute("criteria", l_cri);
			model.addAttribute("emplist", l_listEmployee);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "list_user";
	}
	
	@RequestMapping(value = "/employees/new", method = RequestMethod.GET)
	public String newEmployee(Locale locale, Model model) {
		
		try {
			List<Leavesentitlement> l_enList = leavesentitlementDAO.getAllLeavesentitlement();
			model.addAttribute("entitlements", l_enList);
			model.addAttribute("employee", new Employee());
			model.addAttribute("e_user", new User());
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "user";
	}
	
	@RequestMapping(value = "/employees/*", method = RequestMethod.POST, params = "save")
	public String saveEmployee(Model model, @ModelAttribute("SpringWeb")Employee employee, WebRequest webRequest)
	{
		try {
			if(userDAO.isExisted(employee.getEmployeeID()))
			{
				return "error";
			}
			else
			{
				User l_user = new User();
				l_user.setLoginID(employee.getEmployeeID());
				l_user.setLoginName(employee.getName());
				l_user.setUserStatus(RecordStatus.active);
				l_user.setDefaultPage(webRequest.getParameter("defaultPage"));
				l_user.setType(webRequest.getParameter("type"));
				l_user.setUserStatus(RecordStatus.active);
				l_user.setPassword(webRequest.getParameter("password"));
				userDAO.addUser(l_user);
				
				employee.setLoginID(l_user.getLoginID());
				employee.setStatus(RecordStatus.active);
				employeeDAO.addEmployee(employee);
			}
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "redirect:/employees";
	}
	
	@RequestMapping(value = "/employees/*", method = RequestMethod.POST, params = "edit")
	public String editEmployee(Model model, @ModelAttribute("SpringWeb")Employee employee, WebRequest webRequest)
	{
		User l_user = new User();
		try {
			
			l_user.setLoginID(employee.getEmployeeID());
			l_user.setLoginName(employee.getName());
			l_user.setDefaultPage(webRequest.getParameter("defaultPage"));
			l_user.setType(webRequest.getParameter("type"));
			l_user.setPassword(webRequest.getParameter("password"));
			userDAO.updateUser(l_user);
			
			employee.setLoginID(l_user.getLoginID());
			employee.setStatus(RecordStatus.active);
			employeeDAO.updateEmployee(employee);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		
		if(webRequest.getParameter("profile").equals(""))
			return "redirect:/employees";
		else
			return "redirect:" + l_user.getDefaultPage();
	}
	
	@RequestMapping(value = "/employees/*", method = RequestMethod.POST, params = "delete")
	public String deleteEmployee(Locale locale, Model model, @ModelAttribute("SpringWeb")Employee employee)
	{
		try {
			userDAO.deleteUser(employee.getLoginID());
			employeeDAO.deleteEmployee(employee.getEmployeeID());
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}		
		
		return "redirect:/employees";
	}
	
	@RequestMapping(value = "/employees/{employeeID}", method = RequestMethod.GET)
	public String viewEmployee(Locale locale, Model model, @PathVariable String employeeID)
	{
		try {
			Employee l_employee = employeeDAO.getEmployee(employeeID);
			User l_user = userDAO.getUser(l_employee.getLoginID());
			List<Leavesentitlement> l_enList = leavesentitlementDAO.getAllLeavesentitlement();
			model.addAttribute("entitlements", l_enList);
			model.addAttribute("employee", l_employee != null ? l_employee : new Employee());
			model.addAttribute("e_user", l_user);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		
		return "user";
	}
	
	@RequestMapping(value = "/employees/profile", method = RequestMethod.GET)
	public String viewProfile(Model model, HttpSession session)
	{
		try {
			String l_empID = ((User)session.getAttribute("user")).getLoginID();
			Employee l_employee = employeeDAO.getEmployee(l_empID);
			User l_user = userDAO.getUser(l_empID);
			List<Leavesentitlement> l_enList = leavesentitlementDAO.getAllLeavesentitlement();
			model.addAttribute("entitlements", l_enList);
			model.addAttribute("employee", l_employee != null ? l_employee : new Employee());
			model.addAttribute("e_user", l_user);
			model.addAttribute("isSelf", "true");
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		
		return "user";
	}
}
